import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {
    login,
    logout,
    getInfo,
    weChat
} from '@/api/login'
import {
    getToken,
    setToken,
    removeToken
} from '@/utils/auth'

const baseUrl = config.baseUrl

const user = {
    state: {
        token: getToken(),
        name: storage.get(constant.name),
        avatar: storage.get(constant.avatar),
        roles: storage.get(constant.roles),
        permissions: storage.get(constant.permissions),
        // userInfo: uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo') : {},
        userInfo: storage.get(constant.userInfo),
        location: uni.getStorageSync('location') ? uni.getStorageSync('location') : {}
    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_NAME: (state, name) => {
            state.name = name
            storage.set(constant.name, name)
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
            storage.set(constant.avatar, avatar)
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
            storage.set(constant.roles, roles)
        },
        SET_PERMISSIONS: (state, permissions) => {
            state.permissions = permissions
            storage.set(constant.permissions, permissions)
        },
        SET_USERINFO: (state, userInfo) => {
            state.userInfo = userInfo
            storage.set(constant.userInfo, userInfo)
        },
        SET_LOCATION: (state, location) => {
            state.location = location
        },
    },

    actions: {
        // 登录
        Login({
                  commit
              }, userInfo) {
            const username = userInfo.username.trim()
            const password = userInfo.password
            return new Promise((resolve, reject) => {
                console.log("来登录!")
                login(username, password).then(res => {
                    setToken(res.data.token)
                    commit('SET_TOKEN', res.data.token)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 登录
        wechatLogin({
                        commit
                    }, wechatInfo) {
            return new Promise((resolve, reject) => {
                weChat(wechatInfo).then(res => {
                    console.log(res.data)
                    setToken(res.data.token)
                    commit('SET_TOKEN', res.data.token)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 获取用户信息
        GetInfo({commit, state}) {
            return new Promise((resolve, reject) => {
                getInfo().then(res => {
                    const user = res.data
                    const avatar = (user == null || user.profilePhoto == "" || user.profilePhoto == null) ?
                        "https://saas-v5.oss-cn-guangzhou.aliyuncs.com/saas-admin/druid/mpimg/avatar.png" :
                        user.profilePhoto
                    const username = (user == null || user.nickName == "" || user.nickName ==
                        null) ? "微信用户" : user.nickName
                    commit('SET_NAME', username)
                    commit('SET_AVATAR', avatar)
                    commit('SET_USERINFO', res.data)
                    // proprietorFlag    业主标识:0.否;1.是
                    // lesseeFlag     租客标识:0.否;1.是
                    let roles = []
                    if (res.data.proprietorFlag === '1') {
                        roles.push("PROPRIETOR")
                    }
                    if (res.data.lesseeFlag === '1') {
                        roles.push("TENANT")
                    }
                    commit('SET_ROLES', roles);
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 退出系统
        LogOut({
                   commit,
                   state
               }) {
            return new Promise((resolve, reject) => {
                logout(state.token).then(() => {
                    commit('SET_TOKEN', '')
                    commit('SET_ROLES', [])
                    commit('SET_PERMISSIONS', [])
                    removeToken()
                    storage.clean()

                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }
}

export default user